<?php
/**
 * 用户部门关系表
 */
apf_require_class("Bll_BLL");
apf_require_class("Bll_hr_City");
class Bll_Attendance_ExportDepartment extends BLL{


    private function _get_cities() {
        $bll_city = new Bll_hr_City();
        return $bll_city->get_cities();
    }



    public function find_tree_by_user($user_id) {
        $result = array();
        if(empty($user_id)) {
            return $result;
        }
        $cities  = $this->_get_cities();
        $arr = $this->get_default_dao()->build_data($cities);
        $result['9999999']['user_id'] = $user_id;
        $result['9999999']['user_name'] = '梁伟平';
        $result['9999999']['parent_id'] = '0';
        $result['9999999']['user_job_id'] = '0';
        $result['9999999']['department_name'] = '总裁办公室';
        $result['9999999']['bu_name'] = '总部';
        $result['9999999']['city_name'] = '上海';
        $result['9999999']['department_id'] = '1';

        $child = $this->tree($arr,$user_id);
        if(!empty($child)) {
            $result['9999999']['children'] = $child;
        }else {
            $result['9999999']['leaf'] = 1;
        }
        return $result;
    }

    /*
     *
     <li><span>Item 1</span>
        <ul>
            <li><span>Item 1.0</span>
                <ul>
                    <li><span>Item 1.0.0</span></li>
                </ul>
            </li>
            <li><span>Item 1.1</span></li>
            <li><span>Item 1.2</span>
                <ul>
                    <li><span>Item 1.2.0</span>
                    <ul>
                        <li><span>Item 1.2.0.0</span></li>
                        <li><span>Item 1.2.0.1</span></li>
                        <li><span>Item 1.2.0.2</span></li>
                    </ul>
                </li>
                    <li><span>Item 1.2.1</span>
                    <ul>
                        <li><span>Item 1.2.1.0</span></li>
                    </ul>
                </li>
                    <li><span>Item 1.2.2</span>
                    <ul>
                        <li><span>Item 1.2.2.0</span></li>
                        <li><span>Item 1.2.2.1</span></li>
                        <li><span>Item 1.2.2.2</span></li>
                    </ul>
                </li>
                </ul>
            </li>
        </ul>
    </li>
     *
     */

    public function tree($table,$p_id) {
        $tree = array();
        foreach($table as $id => $row){
            if($row['parent_id']==$p_id){
                $tmp = $this->tree($table,$row['user_id']);
                if($tmp){
                    $row['children']=$tmp;
                }else{
                    $row['leaf'] = true;
                }
                unset($table[$id]);
                $tree[]=$row;
            }
        }
        return $tree;
    }



    public function get_default_dao() {
        return $this->get_dao('Dao_hr_City');
    }
}
